package com.market.plus.core.dao.jdbc;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CommonSQLCommand implements SQLCommand {

   private final static Map<String, Map<String, String>> LIST_SQL_COMMANDS;

   private String databaseType;

   static {
      Map<String, Map<String, String>> maps = new HashMap<String, Map<String, String>>();
      Map<String, String> sqlCommand = new HashMap<String, String>();
      sqlCommand.put("select_InboxNews", "SELECT * from \"InboxNews\" WHERE n_friend_part_id = ?");
      sqlCommand.put("select_Public_InboxNews", "SELECT * from \"InboxNews\" WHERE p_participant_id = :partId and n_friend_part_id IN (:friendIds)");
      maps.put("postgres", sqlCommand);
      LIST_SQL_COMMANDS = maps;
   }

   public String getSelectCommonNewsByParticipantId() {
      String sql = LIST_SQL_COMMANDS.get(this.databaseType).get("select_InboxNews");
      return sql;
   }

   public String getSelectPrivateNewsByParticipantId() {
      String sql = LIST_SQL_COMMANDS.get(this.databaseType).get("select_Public_InboxNews");
      return sql;
   }

   public String getDatabaseType() {
      return databaseType;
   }

   public void setDatabaseType(String databaseType) {
      this.databaseType = databaseType;
   }

   public String convertLongParaValues(List<Long> values) {
      StringBuilder convertPara = new StringBuilder();
      Iterator<Long> entryIter = values.iterator();
      int k = 0;
      while (entryIter.hasNext()) {
         if (k > 0){
            convertPara.append(",");
         }
         k++;
         convertPara.append(entryIter.next());
      }
      return convertPara.toString();
   }

}
